package com.jxzyk.model.users;

import org.json.JSONException;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.core.ui.activity.BaseActivity;
import com.core.util.T;
import com.core.util.UiTools;
import com.jxzyk.R;
import com.jxzyk.entity.User;
import com.jxzyk.http.BaseRequest.OnDialogListener;
import com.jxzyk.http.req.LoginRequest;
import com.jxzyk.model.MainActivity;
import com.jxzyk.util.UserManger;
import com.jxzyk.view.ClearEditText;
import com.jxzyk.view.TitleLayout;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnCompoundButtonCheckedChange;

@ContentView(R.layout.act_login)
public class LoginActivity extends BaseActivity {
	@ViewInject(R.id.et_login_username)
	ClearEditText et_phone;
	@ViewInject(R.id.et_login_passwd)
	ClearEditText et_pwd;
	@ViewInject(R.id.title)
	TitleLayout title;

	// @ViewInject(R.id.cb_login_pwd)
	// CheckBox cb_pwd;
	// @ViewInject(R.id.cb_login_auto)
	// CheckBox cb_auto;

	// @OnCompoundButtonCheckedChange(value = { R.id.cb_login_pwd,
	// R.id.cb_login_auto })
	// public void onCheckedChanged(CompoundButton buttonView, boolean
	// isChecked) {
	// switch (buttonView.getId()) {
	// case R.id.cb_login_pwd:
	// manger.setSave(isChecked);
	// break;
	// case R.id.cb_login_auto:
	// manger.setAuto(isChecked);
	// break;
	// default:
	// break;
	// }
	// }

	@OnClick(value = { R.id.btn_login, R.id.tv_login_regiest })
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			login();
			break;
		case R.id.tv_login_regiest:
			showActivity(RegisterActivity.class);
			break;
		default:
			break;
		}
	}

	private UserManger manger;

	@Override
	public void initData(Bundle bundle) {
		manger = new UserManger(getBaseContext());
	}

	private void login() {
		String phone = UiTools.getString(et_phone);
		if (!verifyPhone(phone)) {
			return;
		}
		String pwd = UiTools.getString(et_pwd);
		if (!UiTools.Empty(pwd)) {
			T.toast("密码输入有误");
			return;
		}
		LoginRequest dialog = new LoginRequest(LoginActivity.this);
		dialog.setParams(phone, pwd);
		dialog.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				try {
					manger.setUserName(UiTools.getString(et_phone));
					manger.saveUser(json.toJSONString());
					manger.setAuto(true);
					User user = manger.getUser();
					// T.toast(user.getUser_name());
					showActivity(MainActivity.class);
					finish();
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(String msg) {
				T.toast("请检查网络设置");
				manger.setAuto(false);
			}
		});
		dialog.show("正在登录...");
	}

	@Override
	public void initUi() {
		title.setTtile("登录");
		initEditText();
		String userName = manger.getUserName();
		et_phone.setText(userName);
		boolean savePwd = manger.getSave();
		try {
			if (savePwd) {
				User user = manger.getUser();
				if (user != null) {
					et_pwd.setText(user.getUser_pwd());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		boolean saveAuto = manger.getAuto();
		if (savePwd && saveAuto) {
			login();
		}
		// cb_pwd.setChecked(savePwd);
		// cb_auto.setChecked(saveAuto);
	}

	public boolean verifyPhone(String phone) {
		if (phone.length() == 11
				&& phone.matches("(13[0-9]|15[6-9]|15[0-3]|18[0-9]|145|147|17[6-8])[0-9]{8}$"))
			return true;
		T.toast("手机号格式有误");
		return false;
	}

	private void initEditText() {
		et_phone.setDrawable(R.drawable.ic_login_phone,
				R.drawable.ic_login_phone_);
		et_pwd.setDrawable(R.drawable.ic_login_pwd, R.drawable.ic_login_pwd_);
	}
}
